﻿using System;
using System.Collections.Generic;
using System.Collections;

using System.Text;
using BuildingStructuralElements.SectionsEC4 ;

namespace BuildingStructuralElements
{

    public class SectionsManager
    {

        private static SectionsManager instance;
        public static SectionsManager Instance
        {
            get
            {
                if (instance == null)
                    instance = new SectionsManager();
                return instance;
            }
        }
        public enum SectionManagerActive {EC4,INSTANT}
        public bool HasLoadedFiles = false;

        public SectionManagerActive ActiveManager;
        SectionManagerEC4 EC4SectionManager;
 

        public bool LoadSectionFile(string path)
        {

            if (path == "") return false;
            string extension = path.Substring(path.Length - 3, 3);

            if (extension == "xml")
            {
                ActiveManager = SectionManagerActive.EC4;
                EC4SectionManager = new SectionManagerEC4();
                EC4SectionManager.LoadSectionFile(path); //INSTANTSectionManager = null;
                HasLoadedFiles = true;
                return true;
            }
            if (extension == "SEC")
            {
                ActiveManager = SectionManagerActive.INSTANT; 
              //  INSTANTSectionManager = new BuildingStructuralElements.Sections.SectionManagerINSTANT();
               // INSTANTSectionManager.LoadSectionFile(path); EC4SectionManager = null;
                HasLoadedFiles = true;
                return true;
            }
            return false;
            HasLoadedFiles = false;

        }
        public Section GetSection(string sectionName)
        {     
  

            if (sectionName == "") return null;
            if (EC4SectionManager != null)
            {
      
                int index = sectionName.IndexOf(" ");
                string type = sectionName.Substring(0, index).Trim();
                string name = sectionName.Substring(index + 1).Trim();

                return EC4SectionManager.GetSection(type, name);

                if (!EC4SectionManager.ExistSection(type, name)) return null;
                System.Data.DataTable table = EC4SectionManager.GetSectionsTable(type); ;

                int index1 = EC4SectionManager.GetSectionIndex(table, type, name);

                #region HEA && IPE
                if (type == "HEA" || type == "IPE")
                {
                    float h;
                    float b;
                    float t;
                    float s;
                    float r;

                    if (type == "HEA")
                    {
                        SteelDataSet.Sections_HEARow row = (SteelDataSet.Sections_HEARow)table.Rows[index1];
                        h = (float)row.h;
                        b = (float)row.b;
                        t = (float)row.tf;
                        s = (float)row.tw;
                        r = (float)row.r;
                    }
                    else
                    {
                        SteelDataSet.Sections_IPERow row = (SteelDataSet.Sections_IPERow)table.Rows[index1];
                        h = (float)row.h;
                        b = (float)row.b;
                        t = (float)row.tf;
                        s = (float)row.tw;
                        r = (float)row.r;
                    }

                    Section Section = new SectionIRolled(sectionName, h, b, t, s, r);
                    //	beam.Section = Section;
                    return Section;
                    /*
                    if (beam is VariableBeam)
                    {
                        VariableBeam b1 = (VariableBeam)beam;
                        b1.BeamEndSection = new SectionIWelded();    
                        b1.Section = new SectionIWelded();    
                    }
                     * */

                }
                #endregion

                #region SHS
                if (type == "SHS")
                {


                    SteelDataSet.Sections_SHSRow row = (SteelDataSet.Sections_SHSRow)table.Rows[index1];

                    float h = (float)row.b;
                    float t = (float)row.t;
                    float re = 3;
                    float ri = 3;

                    Section Section = new SectionSHS(sectionName, h, t, re, ri);
                    //     beam.Section = Section;
                    return Section;

                }
                #endregion

                #region RHS
                if (type == "RHS")
                {


                    SteelDataSet.Sections_RHSRow row = (SteelDataSet.Sections_RHSRow)table.Rows[index1];

                    float h = (float)row.h;
                    float b = (float)row.b;
                    float t = (float)row.t;
                    float re = 3;
                    float ri = 3;

                    Section Section = new SectionRHS(sectionName, h, b, t, re, ri);
                    // beam.Section = Section;
                    return Section;


                }
                #endregion

                #region CHS
                if (type == "CHS")
                {


                    SteelDataSet.Sections_CHSRow row = (SteelDataSet.Sections_CHSRow)table.Rows[index1];

                    float d = (float)row.d;
                    float t = (float)row.t;

                    Section Section = new SectionCHS(sectionName, d, t);
                    // beam.Section =Section ;
                    return Section;

                }

                return null;


                #endregion
            }
         
            return null;

        }

        public List <string > GetListOfSections()
        {         
            List<string> sections = new List<string>();
            if (EC4SectionManager != null)
            {

               System .Data .DataTable table2 =  EC4SectionManager.GetSectionsTable("HEA");
               double b = EC4SectionManager.GetSectionInertiaAttribute("HEA", "100", "b");
                ArrayList list = GetSections("HEA");
                foreach (string  l in list)
                {
                    sections.Add(l);
                }
            }


            return sections;

        }
        private ArrayList GetSections(string type)
        {
            ArrayList list = new ArrayList();
           System .Data .DataTable  table = EC4SectionManager.GetSectionsTable(type);

            if (table != null)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    string symbol = table.Rows[i].ItemArray[1].ToString();
                    string name = table.Rows[i].ItemArray[2].ToString();
                    string str = symbol + " " + name;
                    list.Add(str);
                }
            }

            return list;
        }


    }
}
